package com.dm.hdfxxt.jxsmanage.service.impl;

import com.dm.hdfxxt.jxsmanage.mapper.JxsMapper;
import com.dm.hdfxxt.jxsmanage.service.JxsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class JxsServiceImpl implements JxsService {
    @Autowired
    private JxsMapper jxsMapper;

    /**
         * @Description 保存经销商信息
         * @Author zhangxue
         * @Date 11:19 2018/10/30
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveDealer(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        List<Map<String,Object>> GiftList = jxsMapper.queryDealerByCondition(param);
        if(GiftList.size()>0){
            StatusMap.put("Status",-1);//经销商已存在
            return StatusMap;
        }
        int i = jxsMapper.saveDealer(param);
        if(i==1){
            StatusMap.put("Status",1);//保存成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//保存失败
        }
        return StatusMap;
    }

    /**
     * @Description 编辑经销商信息
     * @Author zhangxue
     * @Date 11:19 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> editDealer(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        int i = jxsMapper.editDealer(param);
        if(i==1){
            StatusMap.put("Status",1);//编辑成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//保存失败
        }
        List<Map<String,Object>> GiftList = jxsMapper.queryDealerByCondition(param);
        if(GiftList.size()>1){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",-1);//经销商已存在
            return StatusMap;
        }
        return StatusMap;
    }

    /**
     * @Description 删除经销商信息
     * @Author zhangxue
     * @Date 11:19 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delDealer(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        int i = jxsMapper.delDealer(param);
        if(i==1){
            StatusMap.put("Status",1);//删除成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//删除失败
        }
        return StatusMap;
    }

    /**
         * @Description 查询所有经销商
         * @Author zhangxue
         * @Date 11:36 2018/10/30
         * @Param
         * @retrun
         **/
    @Override
    public List<Map<String, Object>> queryAllJxs(Map<String, Object> param) {
        List<Map<String,Object>> ResultList = jxsMapper.queryAllJxs(param);
        return ResultList;
    }
}
